package com.etc.controller;

import com.etc.entity.SysOrder;
import com.etc.entity.SysUser;
import com.etc.service.SysOrderService;
import com.etc.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

@Controller
@RequestMapping("adminOder")
public class AdminOderController {

    @Autowired
    private SysOrderService sysOrderService;


    @Autowired
    private SysUserService sysUserService;


    /**
     * 订单页面
     * @return
     */
    @RequestMapping("index")
    public String oder(HttpSession session,HttpServletRequest request){
        SysUser sysUser = (SysUser) session.getAttribute("user");
        request.setAttribute("leiji",sysUserService.findById(String.valueOf(sysUser.getId())).getLeiji());
        return "userAdmin/order";
    }


    /**
     * 添加下单
     * @param out
     * @param sysOrder
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public void order(PrintWriter out, SysOrder sysOrder, HttpSession session){
        Double numter = Double.valueOf(sysOrder.getNumber())*10;
        SysUser sysUser = sysUserService.findById(String.valueOf(sysOrder.getUserId()));

        Double huiY = 0.0;
        int a = Integer.valueOf(sysUser.getLeiji());
        if(a > 0 && a<100){
            huiY = 0.9;
        }
        if(a > 100 && a<300){
            huiY = 0.8;
        }
        if(a > 300 && a<700){
            huiY = 0.7;
        }
        if(a >7000 && a<1000){
            huiY = 0.6;
        }
        if (Double.valueOf(sysUser.getMony()) < numter*huiY){
            out.print(1);
        }else {
            Integer restl =sysOrderService.add(sysOrder,numter*huiY);
            if(restl == 1){
                out.print(2);
            }else {
                out.print(3);
            }
        }
    }


    /**
     * 订单记录页面
     * @return
     */
    @RequestMapping("orderRecord")
    public String orderRecord(HttpSession session, HttpServletRequest request){
        SysUser sysUser = (SysUser) session.getAttribute("user");
        request.setAttribute("order",sysOrderService.get(String.valueOf(sysUser.getId())));
        return "userAdmin/orderRecord";
    }
}
